module.exports = function ($resource, $state, $http, $q, toaster, $modal) {
	return {
		restrict: 'AE',
		template: require('../views/productshop.html'),
		replace: true,
		scope: {
			'saleobj': '=',
			'util': '=',
			// 'open' : '=',
		},
		link: function (scope, elements, attrs) {
			scope.tickettypeobj = {
				'id': '',
				'sale_code': scope.saleobj.code,
				'place_code': '',
				'periodstart': '',
				'periodend': '',
				'type': 'D',
				'appoint': '0'
			};
			scope.periodstart = {'lable': scope.util.date2str(new Date()), 'opened': false };
			scope.periodend = {'lable': new Date().getFullYear() + '-12-31', 'opened': false };
			scope.shopArr = [];
			scope.shopMap = {};
			// scope.ticMap = {};
			//页面需要的数据
			scope.page = {
				'selectViewList': []
			};

			scope.load = function(){
				$resource('/api/as/tc/placeview/jlist', {}, {}).save(scope.tickettypeobj, function (res) {
					if (res.errcode !== 0) {
						toaster.error({ title: "提示", body: res.errmsg });
						return;
					}
					scope.page.viewlist = res.data;
					for (var index = 0; index < res.data.length; index++) {
						var element = res.data[index];
						scope.shopMap[element.code + ''] = element.name;
					}
					gettickettypedetail();
				});
			}
			scope.load();

			//添加票种
			// scope.tempArr = [];
			scope.add = function () {
				var modalInstance = $modal.open({
					template: require('../views/createView.html'),
					controller: 'createView',
					size: 'lg',
					resolve: {
						type: function () {
							return "D"
						}
					}
				});
				//关闭模态框刷新页面
				modalInstance.result.then(function (result) {
					scope.load();
				});
			};

			scope.save = function (obj) {
				if (obj === undefined) {
					if(!scope.page.selectViewList || scope.page.selectViewList.length == 0){
						toaster.warning({title: '', body: '请添加门店'});
						return;
					}
					var periodstart = periodend = "", codeArr = [];
					angular.forEach(scope.page.selectViewList, function(item){
						codeArr.push(item.code);
					});
					if (typeof scope.periodstart.lable === 'string') {
						periodstart = scope.periodstart.lable;
					} else {
						periodstart = scope.util.date2str(scope.periodstart.lable);
					}
					if (typeof scope.periodend.lable === 'string') {
						periodend = scope.periodend.lable;
					} else {
						periodend = scope.util.date2str(scope.periodend.lable);
					}
					
					var para = {
						'name': scope.saleobj.name,
						'sale_code': scope.tickettypeobj.sale_code,
						'place_code': codeArr.toString(),
						'price': 0,
						'periodstart': periodstart,
						'periodend': periodend,
						'type': 'M',
						'appoint': scope.tickettypeobj.appoint,
						'period_scope': ''
					};
					scope.tickettypeobj.place_code = "";

				} else {
					var periodstart = "";
					var periodend = "";
					if (typeof obj.start_date.lable === 'string') {
						periodstart = obj.start_date.lable;
					} else {
						periodstart = scope.util.date2str(obj.start_date.lable);
					}
					if (typeof obj.end_date.lable === 'string') {
						periodend = obj.end_date.lable;
					} else {
						periodend = scope.util.date2str(obj.end_date.lable);
					}

					var para = {
						'id': obj.id,
						'name': obj.name,
						'sale_code': scope.tickettypeobj.sale_code,
						'place_code': obj.place_code,
						'price': obj.price,
						'periodstart': periodstart,
						'periodend': periodend,
						'type': 'M',
						'appoint': obj.appoint,
						'period_scope': obj.period_scope
					};
				}
				
				$resource('/api/as/tc/salettype/save', {}, {}).save(para, function (res) {
					if (res.errcode !== 0) {
						toaster.error({ title: "提示", body: res.errmsg});
						return;
					}
					toaster.success({ title: "提示", body: '修改成功' });
					scope.load();
				});
			};

			scope.addTime = function (obj){
        var toaster1 = toaster;
				var modalInstance = $modal.open({
					template: require('../views/addTime.html'),
					controller: 'addTime',
					size: 'lg',
					resolve: {
						data : function (){
							return {
								'lable': scope.util.date2str(new Date()),
								'opened': false
							}
						},
						'toaster' : function (){
							return toaster1;
						},
						time : function (){
							return obj.period_scope;
						}
					}
				});
				//关闭模态框刷新页面
				modalInstance.result.then(function(result) {
					obj.period_scope = result.toString();
				});
			}

			scope.save2 = function (item) {
				var para = {
					id: item.id
				}
				$resource('/api/as/tc/salettype/save', {}, {})
					.save(para, function (res) {
						if (res.errcode !== 0) {
							toaster.error({ title: "", body: res.errmsg });
							return;
						}
						toaster.success({ title: "提示", body: '修改成功' });
						gettickettypedetail();
					});
			};

			//详细信息	删除
			scope.del1 = function (item, index) {
				var para = {'id': item.id}
				$resource('/api/ac/tc/ticketSaleService/updateDeleteSaleType', {}, {}).save(para, function (res) {
					if (res.errcode !== 0) {
						toaster.error({ title: "", body: res.errmsg });
						return;
					}
					toaster.success({ title: "提示", body: '删除成功' });
					scope.shopArr.splice(index, 1);
				});
			}
			
			//详细信息	删除
			scope.delPlace = function (obj, index) {
				obj.placeArr.splice(index, 1);
				var codeArr = [];
				angular.forEach(obj.placeArr, function(item){
					codeArr.push(item.code);
				});
				obj.place_code = codeArr.toString();
			}

			//获取票种详情
			function gettickettypedetail(){
				$resource('/api/as/tc/salettype/list', {}, {})
					.save({'sale_code': scope.saleobj.code }, function (res) {
						if (res.errcode !== 0) {
							toaster.error({ title: "提示", body: res.errmsg });
							return;
						}
						for (var key in scope.tickettypeobj) {
							if (res.data[0].hasOwnProperty(key)) {
								scope.tickettypeobj[key] = res.data[0][key]
							}
						}
						scope.tickettypeobj['place_code'] = '';
						scope.shopArr = res.data;
						for (var index = 0; index < scope.shopArr.length; index++) {
							var element = scope.shopArr[index];
							scope.shopArr[index].start_date = {
								'lable': element.periodstart? element.periodstart: scope.util.date2str(new Date()),
								'opened': false
							};
							scope.shopArr[index].end_date = {
								'lable': element.periodend? element.periodend: scope.util.date2str(new Date(new Date().getFullYear(), 11, 31)),
								'opened': false
							};
							scope.shopArr[index].place_code = element.view_code;
							var placeArr = element.view_code.split(',');
							for (var i = 0; i < placeArr.length; i++) {
								var element = angular.copy(placeArr[i])
								placeArr[i] = {
									name: scope.shopMap[element],
									code: element
								}
							}
							scope.shopArr[index].placeArr = placeArr;
						}
						// if (!res.data.length) {
						// 	return false;
						// }
						// var tmp = res.data[0];
						// for (var index = 0; index < res.data.length; index++) {
						// 	var element = res.data[index];
						// 	scope.shopArr.push({ 
						// 		code: element.view_code, 
						// 		view_name: scope.shopMap[element.view_code],
						// 		start_date: {
						// 			'lable': element.periodstart? element.periodstart: scope.util.date2str(new Date()),
						// 			'opened': false
						// 		},
						// 		end_date: {
						// 			'lable': element.periodend? element.periodend: scope.util.date2str(new Date()),
						// 			'opened': false
						// 		},
						// 		appoint: element.appoint
						// 	})
						// 	scope.ticMap[element.view_code] = scope.shopMap[element.view_code];
						// 	scope.tickettypeobj.place_code += element.view_code + ',';
						// }
						// var tempArr = res.data[0].view_code.split(',');
						// for (var index = 0; index < tempArr.length; index++) {
						// 	var element = tempArr[index];
						// 	scope.shopArr.push({ code: element, name: scope.shopMap[element] })
						// 	scope.ticMap[element] = scope.shopMap[element];
						// 	scope.tickettypeobj.place_code += element + ',';
						// }
					});
			}

			scope.moveUp = function (index) {
				if (index == 0) {
					toaster.success({ title: "已是最靠前", body: '不能上移' });
					return false;
				}
				var oldValue = scope.shopArr[index].sort;
				var newValue = scope.shopArr[index - 1].sort;
				if (oldValue == newValue) {
					newValue = parseInt(newValue) - 1;
				}
				scope.shopArr[index].sort = newValue;
				scope.shopArr[index - 1].sort = oldValue;
				var para = {
					list: [
						{
							id: scope.shopArr[index].id,
							value: newValue
						},
						{
							id: scope.shopArr[index - 1].id,
							value: oldValue
						}
					]
				}
				$resource('/api/ac/tc/ticketSaleTypeService/updateSort', {}, {}).save(para, function (res) {
					if (res.errcode !== 0) {
						toaster.error({ title: "提示", body: res.errmsg });
						return;
					}
					toaster.success({ title: "提示", body: '修改成功' });
					scope.load();
				});
			}

			scope.moveDown = function (index) {
				if (index == scope.shopArr.length -1) {
					toaster.success({ title: "已是最靠后", body: '不能下移' });
					return false;
				}
				var oldValue = scope.shopArr[index].sort;
				var newValue = scope.shopArr[index + 1].sort;
				if (oldValue == newValue) {
					newValue = parseInt(newValue) + 1;
				}
				scope.shopArr[index].sort = newValue;
				scope.shopArr[index + 1].sort = oldValue;
				var para = {
					list: [
						{
							id: scope.shopArr[index].id,
							value: newValue
						},
						{
							id: scope.shopArr[index + 1].id,
							value: oldValue
						}
					]
				}
				$resource('/api/ac/tc/ticketSaleTypeService/updateSort', {}, {}).save(para, function (res) {
					if (res.errcode !== 0) {
						toaster.error({ title: "提示", body: res.errmsg });
						return;
					}
					toaster.success({ title: "提示", body: '修改成功' });
					scope.load();
				});
			}

			
			scope.dateOpen = function ($event, item) {
				$event.preventDefault();
				$event.stopPropagation();
				item.opened = true;
			};
		}
	};
};

